// Created by WXX on 2021/9/23 9:42
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * 执行用时：76 ms, 在所有 C++ 提交中击败了34.38%的用户
 * 内存消耗：59.8 MB, 在所有 C++ 提交中击败了49.08%的用户
 */
class Solution {
public:
    int numSubarrayProductLessThanK(vector<int> &nums, int k) {
        int res = 0;
        for (int i = 0, j = 0, p = 1; i < nums.size(); i++) {
            p *= nums[i];
            while (j <= i && p >= k) p /= nums[j++];
            res += i - j + 1;
        }
        return res;
    }
};

int main() {

    vector<int> nums = {10, 5, 2, 6};
    cout << Solution().numSubarrayProductLessThanK(nums, 100) << endl;  // 8

    return 0;
}
